<!DOCTYPE html []>
<html>
  <head>
    <meta charset="UTF-8" />
    <meta name="author" content="MarkdownViewer++" />
    <title>README.md</title>
    <style type="text/css">
            
/* Avoid page breaks inside the most common attributes, especially for exports (i.e. PDF) */
td, h1, h2, h3, h4, h5, p, ul, ol, li {
    page-break-inside: avoid; 
}

        </style>
  </head>
  <body>
    <h1 id="replication-code-for-polititcal-polarization-and-secular-trends-in-u.s.public-opinion">Replication Code for "Polititcal Polarization and Secular Trends in U.S. Public Opinion"</h1>
    <p>Last update on 12/21/2018 by Barum Park</p>
    <ul>
      <li>
        <p>Notes on Data and Directories:</p>
        <ol>
          <li>
            <strong>To replicate the analysis</strong>, you need three directories. Let <code>wd.base</code> be the directory into which the files have been downloaded. You have to create the directories <code>~/wd.base/code</code>, <code>~/wd.base/raw_data</code>, and <code>~/wd.base/codebook/</code>.</li>
          <li>After creating these directories, place <strong>all <code>R</code> codes</strong> into <code>~/wd.base/code/</code>.</li>
          <li>
            <strong>Raw data Files</strong> (ANES, GSS, and the random seeds for <code>STAN</code>) should be placed into <code>~wd.base/raw_data</code>. ANES data can be downloaded from "http://www.electionstudies.org". GSS data can be downloaded from <a href="http://gss.norc.org/">http://gss.norc.org/</a>.
<ul><li>ANES 1948-2012 cumulative data file should be saved under the name <code>anes_timeseries_cdf_rawdata.txt</code></li><li>ANES 2016 data file should be saved under the name <code>anes_timeseries_2016_rawdata.txt</code></li><li>GSS 1972-2016 cumulative data file should be saved under the name <code>36797-0001-Data.tsv</code></li><li><code>stan.seeds.csv</code> contains the random seeds that were used in the <code>STAN</code> models and should be placed here as well.</li></ul></li>
          <li>The <strong>codebook</strong> has to be placed into the <code>~/wd.base/codebook</code> directory, as the <code>variable_summary_final.xlsx</code> file is used in the <code>1-1.Prep_Data_NES.R</code> and <code>1-2.Prep_Data_GSS.R</code> code.</li>
          <li>After steps 1) to 4) you have to run the <code>_MAINCODE.R</code> to replicate the analysis.</li>
        </ol>
      </li>
      <li>
        <p>Notes on Running <code>_MAINCODE.R</code>:</p>
        <ol>
          <li>Before running the code you have to replace <code>"working directory here!"</code> string with the path to the <code>wd.base</code> directory mentioned above.</li>
          <li>The replication code is written in a way that running <code>_MAINCODE.R</code> will generate <strong>all</strong> results presented in the paper. However, due to <code>R</code>'s inefficient memory handling, replicating the results in a single run might not be possible, if the machine on which the code is executed has not sufficient memory (even though large objects are repeatedly cleared from the environment in the code). If this is the case, breaking up the loops and running each part of the code separately will probably solve the problem.</li>
          <li>
            <strong>Running the whole replication code is quite time consuming</strong>: running <code>_MAINCODE.R</code> from begining to end takes approximately 3 and a half hours on a modern computer.</li>
          <li>At the start of <code>_MAINCODE.R</code>, you have to specify the number of CPUs to use in running the models in the <code>n.cores</code> object. If you are not sure how many cores your computer has, using <code>n.cores &lt;- parallel::detectCores()</code> might be an option (although not recommended). In the distributed version of the code, <code>n.cores</code> is set to 6.</li>
          <li>Notice that the code involving the <code>rstan::sampling</code> command is written such that <strong>each chain will sample 1,000 posterior draws</strong>. This implies that if you specify <code>n.cores &lt;- 3</code>, you'll have 3,000 posterior samples, instead of the 6,000 that were analyzed in the paper. The numerical results of the analysis might slightly differ, accordingly.</li>
          <li>
            <strong>Exact replication of the results</strong>: Even if the same random seeds are used in estimating the Bayesian models, exact numerical results might slightly differ from the results presented in the paper if the operating system or the interface differs. All models that are presented in the paper were run in <code>R</code> version <code>3.4.2</code> using <code>rstan</code> version <code>2.17.3</code> on a <code>Intel(R) Xeon E5-2690v2 3.0GHz</code> machine with <code>x86_64-centos-linux-gnu (64-bit)</code> operating system. The random seeds are distributed together with the <code>R</code> code.</li>
        </ol>
      </li>
      <li>
        <p>Other Notes:</p>
        <ol>
          <li>Due to the sheer volume of output, traceplots are not reproduced in the replication package. The user might access the produced <code>stanfit</code> objects directly.</li>
          <li>The <code>replication.log</code> file shows the log of the last run of the full <code>_MAINCODE.R</code> code. The <code>sessionInfo()</code> of the run is found at the end of the file, which might be helpful for exact replications. The last date on which the full <code>_MAINCODE.R</code> code was run and numerical results checked is 12/03/2018.</li>
        </ol>
      </li>
    </ul>
  </body>
</html>
